﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProjectEuler.Linq;

namespace ProjectEuler
{
    public struct RomanNumeral : IEquatable<RomanNumeral>
    {
        private readonly Int32 _value;

        public RomanNumeral(Int32 value)
        {
            _value = value;
        }

        public override Int32 GetHashCode()
        {
            return _value;
        }

        public override Boolean Equals(Object obj)
        {
            if (obj is RomanNumeral)
            {
                return Equals((RomanNumeral)(obj));
            }

            return false;
        }

        public Boolean Equals(RomanNumeral other)
        {
            return _value.Equals(other._value);
        }

        public Int32 ToInt32()
        {
            return _value;
        }

        public override String ToString()
        {
            var result = new StringBuilder();
            var n = _value;

            while (n > 1000)
            {
                result.Append('M');
                n -= 1000;
            }

            var log10 = (Int32)Math.Floor(Math.Log10(n));

            foreach (var digit in n.DecimalDigits())
            {
                if (digit == 9)
                {
                    result.Append(PowersOfTen[log10]);
                    result.Append(PowersOfTen[log10 + 1]);
                }
                else if (digit >= 5)
                {
                    result.Append(FiveTimesPowersOfTen[log10]);

                    for (int i = 5; i < digit; i++)
                    {
                        result.Append(PowersOfTen[log10]);
                    }
                }
                else if (digit == 4)
                {
                    result.Append(PowersOfTen[log10]);
                    result.Append(FiveTimesPowersOfTen[log10]);
                }
                else
                {
                    for (int i = 0; i < digit; i++)
                    {
                        result.Append(PowersOfTen[log10]);
                    }
                }

                log10--;
            }

            return result.ToString();
        }

        public static Boolean TryParse(String value, out RomanNumeral result)
        {
            result = new RomanNumeral();

            if (!String.IsNullOrEmpty(value))
            {
                var prevNum = Int32.MaxValue;
                var resultNum = 0;

                for (int i = 0; i < value.Length; i++)
                {
                    var n = 0;

                    if (!NumeralValues.TryGetValue(value[i], out n))
                    {
                        return false;
                    }

                    if (n > prevNum)
                    {
                        resultNum -= (2 * prevNum);
                    }

                    resultNum += n;
                    prevNum = n;
                }

                result = new RomanNumeral(resultNum);
                return true;
            }

            return false;
        }

        public static RomanNumeral Parse(String value)
        {
            RomanNumeral result;

            if (!TryParse(value, out result))
            {
                throw new FormatException();
            }

            return result;
        }

        private static readonly Char[] PowersOfTen = new[]
        {
            'I', 'X', 'C', 'M',
        };

        private static readonly Char[] FiveTimesPowersOfTen = new[]
        {
            'V', 'L', 'D',
        };

        private static readonly Char[] Numerals = new[]
        {
            'I', 'V', 'X', 'L', 'C', 'D', 'M'
        };

        private static readonly IDictionary<Char, Int32> NumeralValues = new Dictionary<Char, Int32>()
        {
            { 'I', 1    },
            { 'V', 5    },
            { 'X', 10   },
            { 'L', 50   },
            { 'C', 100  },
            { 'D', 500  },
            { 'M', 1000 },
        };
    }
}
